package com.cduestc.tools;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class GetDataFromNet {

    public String requestUrl(String myUrl,boolean isConnect){
        if(isConnect == false){
            return null;
        }
        try {
            URL url = new URL(myUrl);
            //建立连接
            HttpURLConnection urlConnect = (HttpURLConnection) url.openConnection();
            urlConnect.setRequestMethod("GET");
            urlConnect.setConnectTimeout(5000);
            urlConnect.setReadTimeout(5000);
            int code = urlConnect.getResponseCode();
            if (code == 200) {
                InputStreamReader inputS = new InputStreamReader(urlConnect.getInputStream());
                BufferedReader buffReader = new BufferedReader(inputS);
                String str = "";
                //拼接字符串
                StringBuilder strBuilder = new StringBuilder();
                while ((str = buffReader.readLine()) != null) {
                    strBuilder.append(str);
                }
                urlConnect.disconnect();
                return strBuilder.toString();
            }

            //返回错误

            }catch(MalformedURLException e1){
                e1.printStackTrace();
            }catch(IOException e2){
                e2.printStackTrace();
            }
        return null;
    }
}
